package com.example.constellation.partnerfrag;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import com.example.constellation.R;
import com.example.constellation.bean.StarBean;
import com.example.constellation.utils.AssetsUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class PartnerFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
    ImageView manIv, womanIv;
    Spinner manSp, womanSp;
    Button prizeBtn, analysisBtn;
    private List<StarBean.StarinfoBean> infoList;
    List<String> nameList;//存放了星座名称的集合
    private Map<String, Bitmap> logoImgMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_partner, container, false);
        initView(view);
//        获取 activity 传递的数据
        Bundle bundle = getArguments();
        StarBean starBean = (StarBean) bundle.getSerializable("info");
        infoList = starBean.getStarinfo();
        nameList = new ArrayList<>();
//        获取适配器所需要的数据源
        for (int i = 0; i < infoList.size(); i++) {
            String name = infoList.get(i).getName();
            nameList.add(name);
        }
//        创建适配器
//        为 R.layout.item_partner_sp 布局文件中的 R.id.item_partner_tv 控件添加数据 nameList
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.item_partner_sp, R.id.item_partner_tv, nameList);
//        设置适配器
        manSp.setAdapter(adapter);
        womanSp.setAdapter(adapter);

//        获取第一个图片资源显示在ImageView上
        String logoname = infoList.get(0).getLogoname();
//        直接获取MainActivity中保存的bitmap集合
        logoImgMap = AssetsUtils.getContentlogoImgMap();
        Bitmap bitmap = logoImgMap.get(logoname);

        manIv.setImageBitmap(bitmap);
        womanIv.setImageBitmap(bitmap);

        return view;
    }

    /* 初始化控件 */
    private void initView(View view) {
        manIv = view.findViewById(R.id.partnerfrag_iv_man);
        womanIv = view.findViewById(R.id.partnerfrag_iv_woman);
        manSp = view.findViewById(R.id.partnerfrag_sp_man);
        womanSp = view.findViewById(R.id.partnerfrag_sp_woman);
        prizeBtn = view.findViewById(R.id.partnerfrag_btn_prize);
        analysisBtn = view.findViewById(R.id.partnerfrag_btn_analysis);
        /* 设置按钮的监听器 */
        prizeBtn.setOnClickListener(this);
        analysisBtn.setOnClickListener(this);
        /* 设置spanner的监听器 */
        manSp.setOnItemSelectedListener(this);
        womanSp.setOnItemSelectedListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.partnerfrag_btn_prize:

                break;
            case R.id.partnerfrag_btn_analysis:
//                获取 spanner 选中的位置
                int manSlePos = manSp.getSelectedItemPosition();
                int womanSlePos = womanSp.getSelectedItemPosition();
//                跳转，传值到星座配对界面
                Intent intent = new Intent(getContext(), PartnerAnalysisActivity.class);
                intent.putExtra("man_name",infoList.get(manSlePos).getName());
                intent.putExtra("man_logoname",infoList.get(manSlePos).getLogoname());
                intent.putExtra("woman_name",infoList.get(womanSlePos).getName());
                intent.putExtra("woman_logoname",infoList.get(womanSlePos).getLogoname());
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//      判断是哪一个spanner被点击了
        switch (adapterView.getId()) {
            case R.id.partnerfrag_sp_man:
                String logoname = infoList.get(i).getLogoname();
                Bitmap bitmap = logoImgMap.get(logoname);
                manIv.setImageBitmap(bitmap);
                break;
            case R.id.partnerfrag_sp_woman:
                logoname = infoList.get(i).getLogoname();
                bitmap = logoImgMap.get(logoname);
                womanIv.setImageBitmap(bitmap);
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}